using CSharpPro._0922;
using CSharpPro._1013;
using CSharpPro._1020;
using CSharpPro._1027;
using CSharpPro._1103;
using User = CSharpPro._1013.User;

namespace CSharpPro
{
    class Program
    {

        static Dictionary<string, User> registeredUsers = new Dictionary<string, User>();

        static void Main(string[] args)
        {
            //0915作业
            Phone myPhone = new Phone("苹果6S plus", 192, 120000000, 6288);
            myPhone.DisplayInFo();

            //0922作业一
            Console.Write("请输入需要判断的年份（整数）：");
            string input1 = Console.ReadLine();
            if (int.TryParse(input1, out int targetYear))
            {
                bool result = Demo1.IsLeapYear(targetYear);
                Console.WriteLine(result ? $"{targetYear}年是闰年" : $"{targetYear}年不是闰年");
            }
            else
            {
                Console.WriteLine("输入无效！请输入正确的整数年份（如2024）。");
            }

            //0922作业二
            Console.Write("请输入出行月份（1-12）：");
            if (int.TryParse(Console.ReadLine(), out int month) && month >= 1 && month <= 12)
            {
                Console.Write("请输入交通方式（航班/动车）：");
                string transport = Console.ReadLine();

                double finalPrice = Demo2.CalculateTravelPrice(month, transport);

                Console.WriteLine($"此旅游行程的最终价格为：{finalPrice:C}");
            }
            else
            {
                Console.WriteLine("月份输入无效，请输入1-12之间的整数！");
            }

            //0929作业一
            Demo3 d = new Demo3();
            d.TestPrint1();

            //0929作业二
            d.TestPrint2();

            //1013作业一
            try
            {
                Console.WriteLine("请输入三个整数，用空格分隔：");
                string input = Console.ReadLine();

                string[] parts = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                if (parts.Length != 3)
                {
                    Console.WriteLine("请确保输入三个整数！");
                    return;
                }

                int x = int.Parse(parts[0]);
                int y = int.Parse(parts[1]);
                int z = int.Parse(parts[2]);

                Demo4 d1 = new Demo4();
                d1.TestPrint1(x, y, z);
            }
            catch (FormatException)
            {
                Console.WriteLine("输入格式错误，请输入有效的整数！");
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误：" + ex.Message);
            }

            //1013作业二
            Demo4 d2 = new Demo4();
            d2.TestPrint2();

            //1013作业三
            Demo4 d3 = new Demo4();
            d3.TestPrint3();

            //1013作业四
            Demo4 d4 = new Demo4();
            d4.TestPrint4();

            //1013作业五
            try
            {
                Console.WriteLine("请输入一个正整数：");
                string input = Console.ReadLine();

                if (int.TryParse(input, out int number) && number > 0)
                {
                    Demo4 d5 = new Demo4();
                    d5.TestPrint5(number);
                }
                else
                {
                    Console.WriteLine("输入无效，请确保输入的是一个正整数！");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("程序执行出错：" + ex.Message);
            }

            //1013作业六
            try
            {
                Console.WriteLine("请输入菱形的行数（正奇数）：");
                string input = Console.ReadLine();

                if (int.TryParse(input, out int rows) && rows > 0)
                {
                    Demo4 d6 = new Demo4();
                    d6.TestPrint6(rows);
                }
                else
                {
                    Console.WriteLine("输入无效，请输入一个正整数");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误：{ex.Message}");
            }

            //1013作业七
            bool isRunning = true;
            while (isRunning)
            {
                Console.WriteLine("\n===== 主菜单 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择功能（1-3）：");

                string input = Console.ReadLine();
                if (!int.TryParse(input, out int choice))
                {
                    Console.WriteLine("请输入有效数字！");
                    continue;
                }

                switch (choice)
                {
                    case 1:
                        Login();
                        break;
                    case 2:
                        Register();
                        break;
                    case 3:
                        isRunning = false;
                        Console.WriteLine("程序已退出，再见！");
                        break;
                    default:
                        Console.WriteLine("请输入1-3之间的数字！");
                        break;
                }
            }

            static void Register()
            {
                Console.WriteLine("\n===== 用户注册 =====");

                string username;
                while (true)
                {
                    Console.Write("请输入用户名：");
                    username = Console.ReadLine()?.Trim();

                    if (string.IsNullOrWhiteSpace(username))
                    {
                        Console.WriteLine("用户名不可为空！");
                        continue;
                    }

                    if (!User.IsValidUsername(username))
                    {
                        Console.WriteLine("用户名格式错误！需3-15个字符，且只能包含字母、数字、下划线");
                        continue;
                    }

                    if (registeredUsers.ContainsKey(username))
                    {
                        Console.WriteLine("用户名已存在！");
                        continue;
                    }

                    break;
                }

                string password;
                while (true)
                {
                    Console.Write("请输入密码：");
                    password = Console.ReadLine()?.Trim();

                    if (string.IsNullOrWhiteSpace(password))
                    {
                        Console.WriteLine("密码不可为空！");
                        continue;
                    }

                    if (!User.IsValidPassword(password))
                    {
                        Console.WriteLine("密码格式错误！需6-20个字符，且包含至少1个大写字母、1个小写字母和1个数字");
                        continue;
                    }

                    break;
                }

                while (true)
                {
                    Console.Write("请确认密码：");
                    string confirmPwd = Console.ReadLine()?.Trim();

                    if (string.IsNullOrWhiteSpace(confirmPwd))
                    {
                        Console.WriteLine("确认密码不可为空！");
                        continue;
                    }

                    if (confirmPwd != password)
                    {
                        Console.WriteLine("两次输入的密码不一致！");
                        continue;
                    }

                    break;
                }

                registeredUsers.Add(username, new User { Username = username, Password = password });
                Console.WriteLine("注册成功！已返回主菜单");
            }

            static void Login()
            {
                Console.WriteLine("\n===== 用户登录 =====");

                Console.Write("请输入用户名：");
                string username = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(username))
                {
                    Console.WriteLine("用户名不可为空！");
                    return;
                }

                if (!registeredUsers.ContainsKey(username))
                {
                    Console.WriteLine("登录失败：用户名不存在");
                    return;
                }

                Console.Write("请输入密码：");
                string password = Console.ReadLine()?.Trim();

                if (string.IsNullOrWhiteSpace(password))
                {
                    Console.WriteLine("密码不可为空！");
                    return;
                }

                User user = registeredUsers[username];
                if (user.Password != password)
                {
                    Console.WriteLine("登录失败：密码错误");
                    return;
                }

                Console.WriteLine($"登录成功！欢迎回来，{username}！");
                Console.WriteLine("已进入系统首页（模拟）");
            }

            //1020作业一冒泡排序
            Console.WriteLine("===== 冒泡排序演示 =====");
            int[] testArray = { 45, 12, 67, 8, 32, 55, 91, 23 };
            Demo5 demo = new Demo5();
            demo.TestPrint1(testArray);
            Console.WriteLine("\n排序完成！");

            //1020作业二
            try
            {
                Student student1 = new Student(1001, "张三", 20, "计算机科学与技术");
                Student student2 = new Student(1002, "李四", 19, "软件工程");

                student1.ShowInfo();
                student2.ShowInfo();

                Console.WriteLine("更新学生1的年龄...");
                student1.UpdateAge(21);
                student1.ShowInfo();
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"错误：{ex.Message}");
            }

            Console.WriteLine("程序执行完毕。");

            //1027作业三上课练习代码：Student、Father、Child、Son 、Demo

            //测试Student1
            Student1 stu1 = new Student1();
            stu1.Name = "张三";
            stu1.Age = 18;
            stu1.Study();
            Student1 stu2 = new Student1
            {
                Name = "李四",
                Age = 20
            };
            stu2.Study();

            //测试Father、Son、Child三者继承关系
            Father childObj = new Child();
            childObj.FatherMethod();
            if (childObj is Child child)
            {
                child.ChildMethod();
            }

            Father sonObj = new Son();
            sonObj.FatherMethod();
            if (sonObj is Son son)
            {
                son.SonMethod();
            }

            //测试Demo6
            Demo6 dm = new Demo6();
            Console.WriteLine("整数相加：" + dm.Add(10, 20));
            Console.WriteLine("小数相加：" + dm.Add(10.5, 20.5));

            //1103作业
            UserServiceTest us = new UserServiceTest();
            bool running = true;

            Console.WriteLine("=== 用户登录注册系统 ===");
            while (running)
            {

                ShowMainMenu();
                string input = Console.ReadLine();

                if (int.TryParse(input, out int choice))
                {
                    switch (choice)
                    {
                        case 1:
                            HandleLogin(us);
                            break;
                        case 2:
                            HandleRegister(us);
                            break;
                        case 3:
                            running = false;
                            Console.WriteLine("系统已退出，再见！");
                            break;
                        default:
                            Console.WriteLine("无效选择，请输入1-3之间的数字");
                            break;
                    }
                }
                else
                {
                    Console.WriteLine("输入错误，请输入有效数字");
                }

                if (running)
                {
                    Console.WriteLine("\n按任意键返回主菜单...");
                    Console.ReadKey();
                    Console.Clear();
                }

                static void ShowMainMenu()
                {
                    Console.WriteLine("\n请选择功能：");
                    Console.WriteLine("1. 登录");
                    Console.WriteLine("2. 注册");
                    Console.WriteLine("3. 退出");
                    Console.Write("请输入选项：");
                }

                static void HandleLogin(IUserService service)
                {
                    Console.WriteLine("\n--- 登录 ---");
                    Console.Write("请输入用户名：");
                    string username = Console.ReadLine();
                    Console.Write("请输入密码：");
                    string password = Console.ReadLine();

                    string result = service.Login(username, password);
                    Console.WriteLine(result);

                    if (result.Contains("成功"))
                    {
                        Console.WriteLine("\n=== 系统首页 ===");
                        Console.WriteLine("欢迎使用本系统！");
                    }
                }

                static void HandleRegister(IUserService service)
                {
                    Console.WriteLine("\n--- 注册 ---");
                    Console.Write("请输入用户名：");
                    string username = Console.ReadLine();
                    Console.Write("请输入密码：");
                    string password = Console.ReadLine();
                    Console.Write("请确认密码：");
                    string confirmPassword = Console.ReadLine();

                    string result = service.Register(username, password, confirmPassword);
                    Console.WriteLine(result);
                }
            }
        }
    }
}
        

                    
    